package com.wgy.blog.controller;

import com.wgy.blog.common.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:Iny
 * @Data：2021/11/8 19:53
 */
@RestController
public class UploadController {

    @RequestMapping(value="upload" ,method = RequestMethod.POST)
    @ResponseBody
    public Result upload1(MultipartFile file, HttpServletRequest request) throws IOException {

        String filename =System.currentTimeMillis()/1000 +  file.getOriginalFilename();
        file.transferTo(new File("H:/培训/JAVA/upload/" + filename));
        Map<String,String> map = new HashMap<>();
        String servletName = request.getServerName();
        int localPort = request.getLocalPort();
//        获取当前文件的映射路径
        String path = request.getScheme() +"://"+ servletName + ":" + localPort + "/avatar/" + filename ;
        map.put("src",path);
        map.put("title",filename);
        return Result.success(path);
    }

}
